package com.bocai.eelly.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.SparseArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.adapter.AccessoriesInquiryAdp;
import com.bocai.eelly.bean.FuliaoInquiryListBean;
import com.bocai.eelly.bean.PlaceBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.net.ApiManage;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;
import com.bocai.eelly.views.ExpandTabView;
import com.bocai.eelly.views.XListView;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * 辅料询价
 */
public class AccessoriesInquiryActivity extends BaseActivity implements XListView.IXListViewListener, AdapterView.OnItemClickListener ,View.OnClickListener{

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.lv_find)
    XListView lvFind;
    @Bind(R.id.tv_search)
    TextView tvSearch;
    @Bind(R.id.search)
    RelativeLayout search;
    @Bind(R.id.expandtab_view)
    ExpandTabView expandtabView;
    private int page = 1;
    private AccessoriesInquiryAdp accessoriesInquiryAdp;
    private List<FuliaoInquiryListBean.DataEntity> dataEntities = new ArrayList<>();
    Observer<String> observer;
    private String province = "";
    private String city = "";
    private ArrayList<String> groups = new ArrayList<>();
    private SparseArray<LinkedList<String>> children = new SparseArray<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_order);
        ButterKnife.bind(this);
        observer = new Observer<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(String string) {
                province = string.split(",")[0];
                city = string.split(",")[1];
                onRefresh();
            }
        };
        initView();
        initVaule();
        initSearch(this, "地区", groups, children, observer);
        initEvent();
    }

    /**
     * 初始化数据
     */
    private void initVaule() {
        String timeline = Utils.getTimeline();
        String sign = Utils.md5(timeline);
        ApiManage.getCity(this, timeline, sign, Utils.getUserId(this, "id"), new Action1<PlaceBean>() {
            @Override
            public void call(PlaceBean placeBean) {
//                T.showShort(AccessoriesInquiryActivity.this, placeBean.getMsg());
                L.d(placeBean.getMsg());
                for (int i = 0; i < placeBean.getData().size(); i++) {
                    groups.add(placeBean.getData().get(i).getP());
                    LinkedList<String> tItem = new LinkedList<>();
                    for (int j = 0; j < placeBean.getData().get(i).getC().size(); j++) {
                        tItem.add(placeBean.getData().get(i).getC().get(j).getTitle());
                    }
                    children.put(i, tItem);
                }
            }
        });

    }

    private void initEvent() {
        lvFind.setXListViewListener(this);
        lvFind.setPullLoadEnable(false);
        lvFind.setPullRefreshEnable(true);
        lvFind.setOnItemClickListener(this);
        search.setOnClickListener(this);
//        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
//            @Override
//            public boolean onMenuItemClick(MenuItem item) {
////                startActivity(new Intent(AccessoriesInquiryActivity.this,PostFactoryActivity.class));
//                return false;
//            }
//        });
    }

    private void initView() {
        getFuliaoInquiry();
        ToolbarHelper.setup(this, R.string.str_title_fuliao_xunjia, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        accessoriesInquiryAdp = new AccessoriesInquiryAdp(this, dataEntities);
//        toolbar.inflateMenu(R.menu.menu_find_order);
        lvFind.setAdapter(accessoriesInquiryAdp);
    }

    private void getFuliaoInquiry() {

        Loading.show(this, getString(R.string.str_loading));
        String timeline = Utils.getTimeline();
        String sign = Utils.md5(timeline);
        getTestApi().getFuliaoInquiry(timeline, sign, Utils.getUserId(this, "id"), "", province, city, page + "", Utils.PAGESIZE + "")
                .compose(this.<FuliaoInquiryListBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<FuliaoInquiryListBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        lvFind.stopRefresh();
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showShort(AccessoriesInquiryActivity.this, R.string.net_error);
                    }

                    @Override
                    public void onNext(FuliaoInquiryListBean fuliaoInquiryListBean) {
                        lvFind.stopRefresh();
                        Loading.dismiss();
                        if (Utils.checkResultCode(fuliaoInquiryListBean.getReturn_code())) {
                            dataEntities.addAll(fuliaoInquiryListBean.getData());
                            accessoriesInquiryAdp.notifyDataSetChanged();
                            if (fuliaoInquiryListBean.getData().size() < Utils.PAGESIZE) {
                                lvFind.setPullLoadEnable(false);
                            }else{
                                lvFind.setPullLoadEnable(true);
                            }

                        } else {
                            T.showShort(AccessoriesInquiryActivity.this, fuliaoInquiryListBean.getMsg());
                        }
                    }
                });
    }

    @Override
    public void onRefresh() {
        page = 1;
        dataEntities.clear();
        lvFind.setPullLoadEnable(false);
        getFuliaoInquiry();
    }

    @Override
    public void onLoadMore() {
        page++;
        getFuliaoInquiry();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.search:
                startActivity(new Intent(this,SearchFuliaoInquiryActivity.class));
                break;
        }
    }
}
